<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>导航钩子</title>
    <script src="../vue.js"></script>
    <script src="../vue-router.js"></script>
</head>

<body>
    <div id="app">
        <h1>导航钩子</h1>
        <p>
            <router-link to="/bar">go to bar </router-link>
            <router-link to="/foo">go to foo</router-link>
        </p>
        <router-view></router-view>
    </div>
    <script>
        const Foo = {
            template: '<p>Foo</p>'
        }
        const Bar = {
            template: '<p>Bar</p>'
        }
        const router = new VueRouter({
            routes: [{
                    path: '/foo',
                    component: Foo
                },
                {
                    path: '/bar',
                    component: Bar
                }
            ]
        });
        router.beforeEach((to, from, next) => {

            console.log(to)
            //to: Route: 即将要进入的目标 路由对象
            //Object {name: undefined, meta: Object, path: "/bar", hash: "", query: Object…}
            console.log(from)
            //from: Route: 当前导航正要离开的路由
            //Object {name: null, meta: Object, path: "/", hash: "", query: Object…}
            console.log(next)
           // 一定要调用该方法来 resolve 这个钩子。执行效果依赖 next 方法的调用参数。
            /*
                function(to) {
                    if (to === false || to instanceof Error) {
                    // next(false) -> abort navigation, ensure current URL
                    this$1.ensureURL(true);
                    abort(to);
                    …
            */
            next()//如果不执行这个函数的话，那么这个路由是无法进行跳转的
            
        })
        const app = new Vue({
            router
        }).$mount('#app');
    </script>
</body>

</html>